package day9;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        String file = "C:\\Git";
        String file2 = "D:\\d1";

        recursiveFiles(new File(file), new File(file2));

    }

    private static void recursiveFiles(File file, File file2) throws IOException {
        //获取当前文件夹下所有文件
        File[] files = file.listFiles();

        //循环
        for (File f : files) {
            //判断当前的文件路径是文件还是文件夹
            if(f.isDirectory()) {
                new File(file2.getAbsolutePath() + "\\" + f.getName()).mkdirs();
                recursiveFiles(f, new File(file2.getAbsoluteFile()+ "\\" + f.getName()));
            }else {
                copy(f, new File(file2.getAbsolutePath() + "\\" + f.getName()));
            }
        }
    }

    public static void copy(File file, File file2) throws IOException {
        FileInputStream fis = new FileInputStream(file);

        Files.copy(fis, Paths.get(String.valueOf(file2)), StandardCopyOption.REPLACE_EXISTING);

    }
}
